package com.epoint.mobileoa.personalized.epointcompany.ui.gzrz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.text.InputType;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.GetCCQKTask;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.GetChuchaibaoxiaomingxiTask;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.GetRZTXInfo;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.GetRiziNeirongTask;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.GetSingleCheckUserInfoTask;
import com.epoint.mobileoa.personalized.epointcompany.model.CheckKaoqinInfoModel;
import com.epoint.mobileoa.personalized.epointcompany.model.GongzuoRzBaoxiaoModel;
import com.epoint.mobileoa.personalized.epointcompany.model.GongzuomingxiModel;
import com.epoint.mobileoa.personalized.epointcompany.model.QingjiaInfoModel;
import com.epoint.mobileoa.personalized.epointcompany.ui.gzrz.task.RZCheckClickTask;
import com.epoint.mobileoa.personalized.epointcompany.ui.gzrz.task.RZCheckSaveSingleDetailTask;
import com.epoint.mobileoa.personalized.epointcompany.ui.gzrz.task.RZReturnClickTask;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.ControlsHelp;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;
import com.epoint.mobileoa.tools.XMLUtil;
import com.epoint.mobileoa.tools.controls.list.ListDisplayView;

/**
 * @author liyc
 * @time 2012-8-28 上午11:34:15
 * @annotation 审核详细页面
 */
public class SinglePersonCheck extends SuperView {

	private LinearLayout llKaoqinFrame,llRzmxFrame,llccxxFrame,llQjFrame;
	private List<CheckKaoqinInfoModel> kqlist = new ArrayList<CheckKaoqinInfoModel>();
	private String UserName;
	private List<GongzuoRzBaoxiaoModel> fymxList;
	private int taskId;//1获取被审核人数据，2保存核准工时,3提交审核,4审核退回

	/**
	 * 审核确认按钮
	 */
	private Button btShqr;

	/**
	 * 退回按钮
	 */
	private Button btTh;

	/**
	 * 点评意见
	 */
	private EditText etDpyj;

	private String RZGuid,RZDate,CheckedUserGuid;
	
	private List<GongzuomingxiModel> rzdetaillist;
	@Override
	public void initControls() {

		btTh = (Button) findViewById(R.id.btTh);
		btTh.setOnClickListener(this);
		etDpyj = (EditText) findViewById(R.id.etDpyj);
		btShqr = (Button) findViewById(R.id.btShqr);
		btShqr.setOnClickListener(this);
		llKaoqinFrame = (LinearLayout) findViewById(R.id.llKaoqinFrame);
		llRzmxFrame = (LinearLayout) findViewById(R.id.llRzmxFrame);
		llccxxFrame = (LinearLayout) findViewById(R.id.llCcqkFrame);
		llQjFrame = (LinearLayout) findViewById(R.id.llQjFrame);
		UserName = getIntent().getStringExtra("UserName");
		CheckedUserGuid = getIntent().getStringExtra("UserGuid");
		RZGuid = getIntent().getStringExtra("RZGuid");
		RZDate = getIntent().getStringExtra("RZDate");
		top_title.setText(UserName);

		taskId = 1;
		showDialog_wait(null);
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("UserGuid", CheckedUserGuid);
		param.put("RZGuid", RZGuid);
		param.put("RZDate", RZDate);
		Task task = new GetSingleCheckUserInfoTask(this,param);
		MainService.addTask(task);

	}

	@Override
	public void onClick(View v) {
		if(v==btShqr)
		{
			taskId = 3;
			showDialog_wait("正在提交...");
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("RZRowGuid", RZGuid);
			param.put("RZDate", RZDate);
			param.put("CheckedUserGuid", CheckedUserGuid);
			param.put("PSContent", etDpyj.getText().toString());
			param.put("list", rzdetaillist);
			Task task = new RZCheckClickTask(this, param);
			MainService.addTask(task);
		}
		else if(v==btTh)
		{
			LayoutInflater inflater = LayoutInflater
					.from(SinglePersonCheck.this);
			LinearLayout ll;
			ll = (LinearLayout) inflater.inflate(
					R.layout.alertinput_editview, null);
			final EditText et = (EditText) ll
					.findViewById(R.id.ai_et);

			//设置只能填写数字
			//et.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

			new AlertDialog.Builder(SinglePersonCheck.this)
			.setIcon(R.drawable.epoint2)
			.setTitle("退回原因")
			.setView(ll)
			.setPositiveButton("确定",new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					taskId = 4;
					showDialog_wait("正在退回...");
					HashMap<String, Object> param = new HashMap<String, Object>();
					param.put("RZDate", RZDate);
					param.put("CheckedUserGuid", CheckedUserGuid);
					param.put("RtnReason", et.getText().toString());
					Task task = new RZReturnClickTask(SinglePersonCheck.this,param);
					MainService.addTask(task);
				}})
				.setNegativeButton("取消", null)
				.show();
		}
	}

	@Override
	public void refresh(Object... param) throws Exception{
		m_Dialog.dismiss();
		if(param[0].toString().equals(MainService.NORMAL)){
			if(taskId==1){
				String bs = param[1].toString();
				String kqinfo = StringHelp.getXMLAtt(bs, "KaoqinInfo");
				if(kqinfo.equals(""))
				{
					TextView tv = new TextView(this);
					tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,25);
					tv.setTextColor(Color.RED);
					tv.setText("无考勤记录！");
					llKaoqinFrame.addView(tv);
				}
				else{
					kqlist = XMLUtil.DomAnalysisCommon("<root>"+kqinfo+"</root>",CheckKaoqinInfoModel.class);

					//测试数据
					/*CheckKaoqinInfoModel c = new CheckKaoqinInfoModel();
					c.address = "江苏国泰新点软件有限公司";
					c.time = "2012-08-22 12:11:11";
					c.Type = "手机考勤";
					kqlist.add(c);
					kqlist.add(c);
					kqlist.add(c);
					kqlist.add(c);
					kqlist.add(c);
					kqlist.add(c);*/

					for(int i=0;i<kqlist.size();i++)
					{
						CheckKaoqinInfoModel ct = kqlist.get(i);
						TextView tv = new TextView(this);
						tv.setText(Html.fromHtml("<font color="+getYGColor()+">["+ct.SourceType+"]&nbsp;&nbsp;"+ct.AttendTime+"</font><br/>" +
								"<font color="+getBlackOrWhiteColor()+">"+ct.AttendLocation+"</font>"));

						llKaoqinFrame.addView(tv);
						llKaoqinFrame.addView(ControlsHelp.getLineTextView(this));
					}
				}

				String rizhimxInfo = StringHelp.getXMLAtt(bs, "gzmxs");
				System.out.println(rizhimxInfo);
				if(!rizhimxInfo.equals(""))
				{
					rzdetaillist = GetRiziNeirongTask.XmlAnalysis("<gzmxs>"+rizhimxInfo+"</gzmxs>");
					
					for(int i=0;i<rzdetaillist.size();i++)
					{
						final int mi = i;
						final GongzuomingxiModel g = rzdetaillist.get(i);
						final TextView tv = new TextView(this);
						tv.setTag(g);
						final String lb = g.TypeName;
						final String xmmc = g.XiangMuMC.equals("")?"<font color=red>&lt;空&gt;</font>":g.XiangMuMC;
						final String rwmc = g.MissionName.equals("")?"<font color=red>&lt;空&gt;</font>":g.MissionName;
						final String wcbl = g.CompletePercent;
						final Double pecd = Double.valueOf(wcbl);
						final String pec = Math.round(pecd)+"%";
						final String gznr = g.GongZuoNR;
						final String gs = g.GongZuoSJ;
						final String hzgs = g.CheckWorkSJ.equals("")?gs:g.CheckWorkSJ;
						
						g.CheckWorkSJ = hzgs;
						rzdetaillist.set(i, g);
						/*tv.setText(Html.fromHtml("<font color="+getBlackOrWhiteColor()+">"+
								"类别："+lb+"<br/>"+
								"项目："+xmmc+"<br/>" +
								"任务："+rwmc+"<br/>"+
								"内容："+gznr+"<br/>"+
								"比例："+pec+"<br/>"+
								"工时："+gs +"<br/></font>"+
								"<font color=red><big>核准工时："+hzgs +
								"<big></font>"));*/
						setRizhiDetailTextView(tv, lb, xmmc, rwmc, gznr, pec, gs, hzgs);
						tv.setOnLongClickListener(new OnLongClickListener() {

							@Override
							public boolean onLongClick(View v) {

								LayoutInflater inflater = LayoutInflater
										.from(SinglePersonCheck.this);
								LinearLayout ll;
								ll = (LinearLayout) inflater.inflate(
										R.layout.alertinput_editview_num, null);
								final EditText et = (EditText) ll
										.findViewById(R.id.ai_et);

								//设置只能填写数字
								//et.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

								new AlertDialog.Builder(SinglePersonCheck.this)
								.setIcon(R.drawable.epoint2)
								.setTitle("核准工时")
								.setView(ll)
								.setPositiveButton("确定",new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										if(StringHelp.isEditTextBlank(et))
										{
											return;
										}

										g.CheckWorkSJ = et.getText().toString();
										rzdetaillist.set(mi, g);
										
										taskId=2;
										Map<String,Object> param = new HashMap<String, Object>();
										g.CheckStaWorkSJ = et.getText().toString();
										param.put("DetailRowGuid", g.Rowguid);
										param.put("CheckWorkSJ", et.getText().toString());
										showDialog_wait("正在保存核准工时...");
										
										Task task = new RZCheckSaveSingleDetailTask(SinglePersonCheck.this, param);
										MainService.addTask(task);
										setRizhiDetailTextView(tv, lb, xmmc, rwmc, gznr, pec, gs, et.getText().toString());
									}}).setNegativeButton("取消", null)
									.show();
								return true;
							}
						});
						tv.setOnTouchListener(new OnTouchListener() {

							@Override
							public boolean onTouch(View v, MotionEvent event) {
								System.out.println(event);
								if(event.getAction()==MotionEvent.ACTION_DOWN)
								{
									tv.setBackgroundColor(getResources().getColor(R.color.lightgreen));
								}
								else if(event.getAction()==MotionEvent.ACTION_UP||
										event.getAction()==MotionEvent.ACTION_MOVE){
									tv.setBackgroundColor(getResources().getColor(R.color.transparent));
								}
								return false;
							}
						});
						llRzmxFrame.addView(tv);
						llRzmxFrame.addView(ControlsHelp.getLineTextView(this));
					}
				}


				String chuchaiInfo = StringHelp.getXMLAtt(bs, "ChuChaiInfo");
				if(!chuchaiInfo.equals(""))
				{
					String ccdq = StringHelp.getXMLAtt(chuchaiInfo, "Area");
					String ccxxdz = StringHelp.getXMLAtt(chuchaiInfo, "Address");
					String StartTime = StringHelp.getXMLAtt(chuchaiInfo, "StartTime");
					String EndTimte = StringHelp.getXMLAtt(chuchaiInfo, "EndTimte");
					final String fymxs =  StringHelp.getXMLAtt(chuchaiInfo, "fymxs");

					TextView tv = new TextView(this);
					tv.setText(Html.fromHtml("<font color="+getBlackOrWhiteColor()+">出差地区："+ccdq+"<br/>" +
							"详情地址："+ccxxdz+"<br/>" +
							"起止时间："+StartTime+"至"+EndTimte+"</font>"));
					Button btn = new Button(this);
					btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
					btn.setText("当日费用");
					btn.setBackgroundResource(R.drawable.button_login);
					btn.setTextColor(Color.WHITE);
					btn.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							if(fymxs.equals(""))
							{
								ToastHelp.showToast(SinglePersonCheck.this, "暂无当日费用明细");
							}
							else{
								fymxList = GetCCQKTask.XmlAnalysis("<fymxs>"+fymxs+"</fymxs>");
								ListDisplayView.adapter = new FymxAdapter();

								Intent intent = new Intent(SinglePersonCheck.this, ListDisplayView.class);
								intent.putExtra("title", "当日费用["+UserName+"]");
								startActivity(intent);
							}
						}
					});
					llccxxFrame.addView(tv);
					llccxxFrame.addView(ControlsHelp.getLineTextView(this));
					llccxxFrame.addView(btn);
				}
				else{
					findViewById(R.id.tvCcqkTip).setVisibility(View.GONE);
				}

				String QingjiaInfo = StringHelp.getXMLAtt(bs, "QingjiaInfo");
				if(QingjiaInfo.equals(""))
				{
					findViewById(R.id.tvQjTip).setVisibility(View.GONE);
				}
				else{
					String qjxml = StringHelp.getXMLAttOut(bs, "QingjiaInfo");

					TextView tv = new TextView(this);
					tv.setText(Html.fromHtml("<font color="+getBlackOrWhiteColor()+">" +
							"请假人："+StringHelp.getXMLAtt(qjxml, "QingjiaRen")+"<br/>" +
							"请假时间："+StringHelp.getXMLAtt(qjxml, "QingjiaTime")+"<br/>" +
							"事由："+StringHelp.getXMLAtt(qjxml, "Remark")+"</font>"));
					llQjFrame.addView(tv);
				}

				String LvyouInfo = StringHelp.getXMLAtt(bs, "LvyouInfo");
				if(LvyouInfo.equals(""))
				{
					findViewById(R.id.tvLyTip).setVisibility(View.GONE);
				}
				else
				{

				}
			}
			else if(taskId==2)
			{
				if(!param[1].toString().startsWith("<result>true</result>"))
				{
					ToastHelp.showToast(SinglePersonCheck.this, "核准工时保存失败");
				}
			}
			else if(taskId==3)
			{
				if(param[1].toString().startsWith("<result>true</result>"))
				{
					ToastHelp.showToast(SinglePersonCheck.this, "审核成功");
					RizhiShenHeDetailView.backTag = 1;
					finish();
				}
			}
			else if(taskId==4)
			{
				if(param[1].toString().startsWith("<result>true</result>"))
				{
					ToastHelp.showToast(SinglePersonCheck.this, "退回成功");
					RizhiShenHeDetailView.backTag = 1;
					finish();
				}
			}
		}
	}



	private void setRizhiDetailTextView(TextView tv,String lb,String xmmc,String rwmc,String gznr,String pec,String gs,String hzgs)
	{
		tv.setText(Html.fromHtml("<font color="+getBlackOrWhiteColor()+">"+
				"类别："+lb+"<br/>"+
				"项目："+xmmc+"<br/>" +
				"任务："+rwmc+"<br/>"+
				"内容："+gznr+"<br/>"+
				"比例："+pec+"<br/>"+
				"工时："+gs +"<br/></font>"+
				"<font color=red><big>核准工时："+hzgs +
				"<big></font>"));
	}

	class FymxAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			return fymxList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			GongzuoRzBaoxiaoModel c = fymxList.get(position);
			TextView tv = new TextView(SinglePersonCheck.this);
			tv.setText(Html.fromHtml("<font color="+getYGColor()+">"+c.BXTypeStr+"&nbsp;&nbsp;"+c.FaShengDi+"&nbsp;&nbsp;"+c.BXMoney+"元"+"</font><br/>" +
					"<font color="+getBlackOrWhiteColor()+">"+c.Remarks+"</font>"));
			return tv;
		}

	}

	@Override
	public int getLayoutInt() {
		return R.layout.epointsinglepersoncheck;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param){
	}

}
